package gui;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import rsshunter.RSSEntry;

/**
 * Model of Table which is containing RSSEntries. It's displaying their title
 * and chanel uri.
 */
public class EntriesTableModel extends AbstractTableModel {

    private String[] columnNames = {"Title", "Channel"};
    private List<RSSEntry> data = new ArrayList<RSSEntry>();

    public void addRow(RSSEntry row) {
        data.add(row);
    }

    public List<RSSEntry> getData() {
        return data;
    }

    public void removeByIndexes(int[] indexes) {
        int count = 0;
        for (int index : indexes) {
            data.remove(index - count);
            count++;
        }
    }

    public void removeByIndex(int index) {
        data.remove(index);
    }

    public void clear() {
        int size = data.size();
        data.clear();
        fireTableRowsDeleted(0, size);
    }

    public RSSEntry getEntry(int rowIndex) {
        return data.get(rowIndex);
    }

    public void setData(List<RSSEntry> data) {
        this.data = data;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column];
    }

    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0:
                return data.get(rowIndex).getTitle();
            case 1:
                return data.get(rowIndex).getChannel();
            default:
                return "Bad column index";
        }
    }
}
